package com.csit.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.csit.bean.Pager;
import com.csit.entity.Product;
import com.csit.service.ProductService;
import com.csit.util.ResponseMsgUtil;
import com.csit.util.StringUtil;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
public class ProductAction extends BaseAction implements ModelDriven<Product> {

	private static final long serialVersionUID = 6360418936837594196L;

	private Product product = new Product();
	
	@Resource
	private ProductService productService;
	
	public Product getModel() {
		return product;
	}
	
	public void add(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(product.getProductItemID()==null){
			msgUtil.setMsg("商品种类不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(product.getProductCode())){
			msgUtil.setMsg("商品编号不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(product.getProductName())){
			msgUtil.setMsg("商品名称不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(product.getProductPrice()==null||product.getProductPrice()<=0){
			msgUtil.setMsg("单价必须为正数");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		product.setShop(getShop());
		if(!productService.isUnique(product)){
			msgUtil.setMsg("编号已存在");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(product.getCurrentQuantity()==null){
			product.setCurrentQuantity(0d);
		}
		productService.saveEntity(product);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void update(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(product.getProductID()==null){
			msgUtil.setMsg("商品种类不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(product.getProductCode())){
			msgUtil.setMsg("商品编号不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(product.getProductName())){
			msgUtil.setMsg("商品名称不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(!productService.isUnique(product)){
			msgUtil.setMsg("编号已存在");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(product.getCurrentQuantity()==null){
			product.setCurrentQuantity(0d);
		}
		productService.updateEntity(product);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void select(){
		product.setShop(getShop());
		Pager pager = new Pager(page, rows);
		String jsonString = productService.queryProduct(pager, product);
		ajaxJson(jsonString);
	}
	
	public void get(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(true);
		product = productService.getEntity(product.getProductID());
		msgUtil.setProperty("productID", product.getProductID());
		msgUtil.setProperty("productCode", product.getProductCode());
		msgUtil.setProperty("productName", product.getProductName());
		msgUtil.setProperty("productSize", product.getProductSize());
		msgUtil.setProperty("productPrice", product.getProductPrice());
		msgUtil.setProperty("productItemID", product.getProductItemID());
		msgUtil.setProperty("unitID", product.getUnitID());
		msgUtil.setProperty("note", product.getNote());
	}
	
	/**
	 * 
	 * @Description: 盘点
	 * @param
	 * @Create: 2012-12-8 下午09:04:43
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void inventory(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(true);
		Product model = productService.getEntity(product.getProductID());
		model.setCurrentQuantity(product.getCurrentQuantity());
		productService.updateEntity(model);
		ajaxJson(msgUtil.toJSON());
	}
}
